package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.dao.IndustryInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.IndustryInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.service.IndustryInfoService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author LiuYuLiang.LL
 */
@Service
public class IndustryInfoServiceImpl extends ServiceImpl<IndustryInfoMapper, IndustryInfo> implements IndustryInfoService {
    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Override
    public IPage<IndustryInfo> findByPage(QueryRequest request, IndustryInfo info) {
        try {
            LambdaQueryWrapper<IndustryInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getIndustryType())) {
                queryWrapper.like(IndustryInfo::getIndustryType, info.getIndustryType());
            }
            if (StringUtils.isNotBlank(info.getIndustryName())) {
                queryWrapper.like(IndustryInfo::getIndustryName, info.getIndustryName());
            }
            if (StringUtils.isNotBlank(info.getIndustryDesc())) {
                queryWrapper.like(IndustryInfo::getIndustryDesc, info.getIndustryDesc());
            }
            queryWrapper.orderByDesc(IndustryInfo::getCreateTime);
            Page<IndustryInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }

    @Override
    public IPage<IndustryInfo> indusSelect2List(QueryRequest request, IndustryInfo q) {
        try {
            LambdaQueryWrapper<IndustryInfo> queryWrapper = new LambdaQueryWrapper<>();
//            q = q.replaceAll("&amp;", "&");
//            String[] param = q.split("&");
//            if (param.length > 0) {
//                queryWrapper.or(qw -> qw.like(IndustryInfo::getIndustryType, param[0])
//                        .or().like(IndustryInfo::getIndustryName, param[0])
//                        .or().like(IndustryInfo::getIndustryDesc, param[0]));
//
//            }
//            if (param.length > 1) {
//                queryWrapper.like(IndustryInfo::getGrpNo, param[1]);
//            }
            if (StringUtils.isNotBlank(q.getIndustryDesc())) {
                queryWrapper.or(qw -> qw.like(IndustryInfo::getIndustryType, q.getIndustryDesc())
                        .or().like(IndustryInfo::getIndustryName, q.getIndustryDesc())
                        .or().like(IndustryInfo::getIndustryDesc, q.getIndustryDesc()));
            }
            if (StringUtils.isNotBlank(q.getGrpNo())) {
                queryWrapper.like(IndustryInfo::getGrpNo, q.getGrpNo());
            }
            queryWrapper.eq(IndustryInfo::getStatus, BusinessContainer.data_sta_01);
            Page<IndustryInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }

    @Override
    public List<IndustryInfo> findByIndustryType(String industryType, String status) {
        LambdaQueryWrapper<IndustryInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IndustryInfo::getIndustryType, industryType);
        queryWrapper.eq(IndustryInfo::getStatus, status);
        return this.list(queryWrapper);
    }

    @Override
    @Transactional
    public void create(IndustryInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        entity.setStatus(BusinessContainer.data_sta_01);
        entity.setAuditStatus(BusinessContainer.status_02);
        entity.setOprId(user.getId());
        entity.setCrtOrgId(user.getOrgCode());
        entity.setCreateTime(lastUpddt);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(lastUpddt);
        this.baseMapper.insert(entity);
    }

    @Override
    @Transactional
    public void update(IndustryInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        // 修改
        entity.setAuditStatus(BusinessContainer.status_42);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(lastUpddt);
        this.baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void cancel(String id) {
        // 注销
        IndustryInfo entity = this.getById(id);
        entity.setStatus(BusinessContainer.data_sta_03);
        entity.setAuditStatus(BusinessContainer.status_12);
        this.baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void delete(String[] ids) {
        List<String> list = Arrays.asList(ids);
        this.baseMapper.deleteBatchIds(list);
    }

    @Override
    public IndustryInfo findOneByCode(String industryType, String status) {
        LambdaQueryWrapper<IndustryInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IndustryInfo::getIndustryType, industryType);
        queryWrapper.eq(IndustryInfo::getStatus, status);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据行业编码查询
     *
     * @param industryType 行业编码
     * @return
     */
    @Override
    public IndustryInfo findAllindusByInTp(String industryType) {
        LambdaQueryWrapper<IndustryInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IndustryInfo::getIndustryType, industryType);
        return this.getOne(queryWrapper);
    }

    @Override
    public Map<String, List<String>> getIndustryInfos(List listMerGrp) {
        Map<String, List<String>> siteMap = new HashMap<String, List<String>>();
        for (int p = 0; p < listMerGrp.size(); p++) {
            Map mapMerGrp = (Map) listMerGrp.get(p);
            String grpno = (String) mapMerGrp.get("GRPNO");
            String grpdesc = (String) mapMerGrp.get("GRPDESC");
            List listIndustry = getIndustryInfos(grpno);
            siteMap.put(grpdesc, listIndustry);
        }
        return siteMap;
    }

    public List getIndustryInfos(String grpno) {
        LambdaQueryWrapper<IndustryInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IndustryInfo::getStatus, "01");
        queryWrapper.eq(IndustryInfo::getGrpNo, grpno);
        List<IndustryInfo> industryInfos = this.list(queryWrapper);
        List<String> industryInfoList = new ArrayList<>();
        if (null != industryInfos && industryInfos.size() > 0) {
            industryInfos.forEach((e) -> {
                industryInfoList.add(e.getIndustryType() + "-" + e.getIndustryName());
            });
        }
        return industryInfoList;
    }
}
